#include "StdMyString.h"
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
StdMyString::StdMyString()
{
    std::cout << "无参构造" << std::endl;
    this->size = 10;
    this->len = 0;
    this->str = new char[this->size];
    memset(str,0,10);
}

StdMyString::StdMyString(const char *str)
{
    std::cout << "有参构造" << std::endl;
    this->len = strlen(str);
    this->size = (this->len / 10 + 1) * 10;
    this->str = new char[this->size];
    strcpy(this->str, str);
}

StdMyString::StdMyString(const StdMyString &str)
{
    std::cout << "拷贝" << std::endl;
    this->len = str.len;
    this->size = str.size;
    this->str = new char[this->size];
    strcpy(this->str, str.str);
}

StdMyString::~StdMyString()
{
    std::cout << "析构" << std::endl;
    delete str;
}

bool StdMyString::operator<(const char *str)
{
    if (strcmp(this->str, str) < 0)
        return true;
    return false;
}

bool StdMyString::operator<(const StdMyString &str)
{
    if (strcmp(this->str, str.str) < 0)
        return true;
    return false;
}

bool StdMyString::operator>(const char *str)
{
    if (strcmp(this->str, str) > 0)
        return true;
    return false;
}

bool StdMyString::operator>(const StdMyString &str)
{
    if (strcmp(this->str, str.str) > 0)
        return true;
    return false;
}

bool StdMyString::operator!=(const char *str)
{
    return !(*this == str);
}

bool StdMyString::operator==(const char *str)
{
    if (strcmp(this->str, str) == 0)
        return true;
    return false;
}

bool StdMyString::operator==(const StdMyString &str)
{
    if (strcmp(this->str, str.str) == 0)
        return true;
    return false;
}

bool StdMyString::operator!=(const StdMyString &str)
{
    return !(*this == str);
}


StdMyString StdMyString::GetString(int start, int end)
{
    if (start >= 0 && start < end && end <= this->len)
    {
        StdMyString temp;
        delete temp.str;
        temp.len = end - start;
        temp.size = (temp.len / 10 + 1) * 10;
        temp.str = new char[temp.size];
        strncpy(temp.str,&this->str[start],temp.len);
        temp.str[temp.len] = '\0';
        return temp;
    }
    return StdMyString();
}


void StdMyString::InsertByIndex(int index, const char *str)
{
    if (index < 0 || index > this->len)
    {
        std::cout << "非法位置:" << index << std::endl;
        return;
    }
    else
    {
        char *newStr = new char[this->len + strlen(str)];

        sprintf(newStr, "%s%s%s",this->GetString(0,index).str,str,this->GetString(index,this->len).str);
        delete this->str;
        this->str = newStr;
        this->len = this->len + strlen(str);
        this->size = (this->len / 10 + 1) * 10;
    }
}

void StdMyString::InsertByIndex(int index, const StdMyString &str)
{
    this->InsertByIndex(index,str.str);
}

StdMyString::operator int()
{
    return atoi(this->str);
}

StdMyString::operator float()
{
    return atof(this->str);
}

StdMyString StdMyString::Number(int value)
{
    StdMyString s;
    sprintf(s.str,"%d",value);
    s.len = strlen(s.str);
    return s;
}

StdMyString StdMyString::Number(float value)
{
    StdMyString s;
    sprintf(s.str,"%.2f",value);
    s.len = strlen(s.str);
    return s;
}

char &StdMyString::operator[](int index)
{
    if (index < 0 || index >= this->len)
    {
        std::cout << "operator[]:非法位置" << std::endl;
        return this->str[len + 1];
    }
    else
    {
        return this->str[index];
    }
}

char StdMyString::At(int index)
{
    if (index < 0 || index >= this->len)
    {
        std::cout << "At:非法位置" << std::endl;
        return -1;
    }
    else
    {
        return this->str[index];
    }
}

bool StdMyString::Contains(const char *str)
{
    if (strstr(this->str, str) == NULL)
        return false;
    return true;
}

bool StdMyString::Contains(const StdMyString &str)
{
    return this->Contains(str.str);
}

int StdMyString::Find(const char *str)
{
    char *ptr = strstr(this->str, str);
    if (ptr == nullptr)
        return -1;
    else
        return ptr - this->str;
}

int StdMyString::Find(const StdMyString &str)
{
    return this->Find(str.str);
}

void StdMyString::operator+=(const StdMyString &str)
{
    std::cout << "对象+=对象" << std::endl;
    *this += str.str;
}

void StdMyString::operator+=(const char *str)
{
    std::cout << "对象+=字符串" << std::endl;
    if (this->size > this->len + strlen(str))
    {
        strcat(this->str, str);
    }
    else
    {
        this->len = strlen(str) + this->len;
        this->size = (len / 10 + 1) * 10;
        char *newStr = new char[this->size];
        strcpy(newStr, this->str);
        strcat(newStr, str);
        delete this->str;
        this->str = newStr;
    }
}

StdMyString &StdMyString::operator=(const char *str)
{
    std::cout << "对象=字符串" << std::endl;
    delete this->str;
    this->len = strlen(str);
    this->size = (this->len / 10 + 1) * 10;
    this->str = new char[this->size];
    strcpy(this->str, str);
    return *this;
}

StdMyString &StdMyString::operator=(const StdMyString &str)
{
    std::cout << "对象=对象" << std::endl;
    this->size = str.size;
    this->len = str.len;
    delete this->str;

    this->str = new char[this->size];
    strcpy(this->str, str.str);
    return *this;
}

StdMyString StdMyString::operator+(const char *str)
{
    std::cout << "对象+字符串" << std::endl;
    StdMyString newstr;
    delete newstr.str;
    newstr.len = this->len + strlen(str);
    newstr.size = (newstr.len / 10 + 1) * 10;
    newstr.str = new char[newstr.size];
    strcpy(newstr.str, this->str);
    strcat(newstr.str, str);

    return newstr;
}

StdMyString StdMyString::operator+(const StdMyString &str)
{
    std::cout << "对象+对象" << std::endl;
    StdMyString newstr;
    delete newstr.str;
    newstr.len = this->len + strlen(str.str);
    newstr.size = (newstr.len / 10 + 1) * 10;
    newstr.str = new char[newstr.size];
    strcpy(newstr.str, this->str);
    strcat(newstr.str, str.str);

    return newstr;
}

std::ostream &operator<<(std::ostream &out, const StdMyString &str)
{
    std::cout << "重载<<" << std::endl;
    out << str.str;
    return out;
}

std::istream &operator>>(std::istream &in,StdMyString &str)
{
    char s[Max_cin];
    in>>s;
    delete str.str;

    str.len = strlen(s);
    str.size = (str.len / 10 + 1) * 10;
    str.str = new char[str.size];
    strcpy(str.str,s);
    return in;
}
